using B.S.NewMedical.Api.Read.Application.Command.ContentManagement.Hospital;
using B.S.NewMedical.Api.Read.DTOS.Hospital;
using B.S.NewMedical.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;

namespace B.S.NewMedical.Api.Read.Controllers.ContentManagement.Hospital
{
    /// <summary>
    /// 医院查询控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HospitalController : ControllerBase
    {
        private readonly IMediator _mediator;

        public HospitalController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 搜索医院（支持医院名称模糊查询、地区筛选、分页）
        /// </summary>
        /// <param name="hospitalName">医院名称（模糊搜索）</param>
        /// <param name="region">地区</param>
        /// <param name="pageIndex">当前页码，默认为1</param>
        /// <param name="pageSize">页容量，默认为10</param>
        /// <returns>医院分页列表（包含医院基本信息和科室设置）</returns>
        [HttpGet]
        public async Task<ApiPageing<HospitalDto>> SearchHospitals(
            [FromQuery] string? hospitalName = null,
            [FromQuery] string? region = null,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10)
        {
            var command = new SearchHospitalCommand
            {
                HospitalName = hospitalName,
                Region = region,
                PageIndex = pageIndex,
                PageSize = pageSize
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取推荐医院（只返回推荐的医院）
        /// </summary>
        /// <param name="pageIndex">当前页码，默认为1</param>
        /// <param name="pageSize">页容量，默认为10</param>
        /// <returns>推荐医院分页列表</returns>
        [HttpGet]
        public async Task<ApiPageing<HospitalDto>> GetRecommendedHospitals(
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10)
        {
            var command = new SearchHospitalCommand
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };

            // 通过搜索API获取推荐医院，结果会按推荐状态排序
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据地区获取医院列表
        /// </summary>
        /// <param name="region">地区名称</param>
        /// <param name="pageIndex">当前页码，默认为1</param>
        /// <param name="pageSize">页容量，默认为10</param>
        /// <returns>指定地区的医院分页列表</returns>
        [HttpGet]
        public async Task<ApiPageing<HospitalDto>> GetHospitalsByRegion(
            string region,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10)
        {
            var command = new SearchHospitalCommand
            {
                Region = region,
                PageIndex = pageIndex,
                PageSize = pageSize
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据ID获取医院详情（包含完整的医院信息和科室设置）
        /// </summary>
        /// <param name="id">医院ID</param>
        /// <returns>医院详情信息，包含科室设置</returns>
        [HttpGet]
        public async Task<ApiResult<HospitalDto>> GetHospitalById(int id)
        {
            var command = new GetHospitalByIdCommand(id);
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取医院类型列表（用于下拉框选择）
        /// </summary>
        /// <returns>医院类型列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<HospitalTypeDto>>> GetHospitalTypes()
        {
            var command = new GetHospitalTypesCommand();
            return await _mediator.Send(command);
        }

    }
}